package com.txc.books.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txc.books.entity.BookType;
import com.txc.books.entity.User;
import com.txc.books.mapper.BookMapper;
import com.txc.books.mapper.BookTypeMapper;
import com.txc.books.service.IBookTypeService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import com.txc.books.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: XCTANG
 *
 */

@Service
public class BookTypeServiceImpl implements IBookTypeService {

    private final BookTypeMapper bookTypeMapper;

    public BookTypeServiceImpl(BookTypeMapper bookTypeMapper){
        this.bookTypeMapper = bookTypeMapper;
    }

    @Override
    public List<BookType> findAll() {
        return bookTypeMapper.findAll();
    }

    @Override
    public void insert(BookType bookType) {
        String name = bookType.getName();
        String typeId = bookType.getTypeId();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        if (StringUtils.isEmpty(typeId)){
            throw new IllegalArgumentException("类型不能为空");
        }
        if (StringUtils.isBlank(typeId)){
            throw new IllegalArgumentException("类型不能为空白");
        }
        bookTypeMapper.insert(bookType);
    }

    @Override
    public void update(BookType bookType) {

        String name = bookType.getName();
        String typeId = bookType.getTypeId();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        if (StringUtils.isEmpty(typeId)){
            throw new IllegalArgumentException("类型不能为空");
        }
        if (StringUtils.isBlank(typeId)){
            throw new IllegalArgumentException("类型不能为空白");
        }
        bookTypeMapper.update(bookType);
    }

    @Override
    public void delete(Integer id) {
        bookTypeMapper.delete(id);
    }

    @Override
    public PageResult findByPager(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    public PageInfo<BookType> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BookType> typeMenus = bookTypeMapper.selectPage();
        return new PageInfo<BookType>(typeMenus);
    }
}
